package com.xhg.controller;


import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.xhg.common.Constants;
import com.xhg.common.PageNavation;
import com.xhg.model.User;
import com.xhg.util.safe.MD5Util;


public class UserController extends BaseController<User>{
	
	private User userService=User.dao;
	
	/**
	 * 查询全部用户页面
	 */
	public void all() {
		setAttr("master","user");
		renderJsp("all.jsp");
	}
	/**
	 * 查询用户集合JSON
	 */
	public void getData(){
		PageNavation page=new PageNavation();
		paramMap=getParamMap();
		page.setParamMap(paramMap);
//		page.setPageNumber(Integer.parseInt(paramMap.get("offset").toString()));//获取当前页码
		page.setPageNumber(1);//获取当前页码
		page.setPageSize(Integer.parseInt(paramMap.get("limit").toString()));//获取每页显示数量
		
		Page<User> userPage=userService.queryList(page);//获取集合对象
		StringBuilder sb=new StringBuilder();
		sb.append("{\"total\":"+userPage.getTotalRow()+",");
		sb.append("\"rows\":"+JsonKit.toJson(userPage.getList())+"}");
		renderJson(sb.toString());
	}
    /**
     * 跳转到添加页面
     */
    public void toAdd(){
		setAttr("master","user");
        renderJsp("toAdd.jsp");
    }
    /**
     * 添加
     */
    public void add(){
    	User user=getModel(User.class,"user");
    	user.set("password",  MD5Util.getEncrypt(user.getStr("password")));
        renderJson(userService.save(user));
    }
    /**
     * 跳转到修改页面
     */
    public void toUpd(){
    	Long id=getParaToLong(0);
    	setAttr("master","user");
        setAttr("user",userService.queryById(id));
        render("toUpd.jsp");
    }
    /**
     * 修改
     */
    public void upd() {
    	User user=getModel(User.class,"user");
    	User obj=new User().set("id", user.getLong("id")).set("userName",user.get("userName"));
    	if(user.getStr("password")!=null){
    		obj.set("password",  MD5Util.getEncrypt(user.getStr("password")));
    	}
    	renderJson(userService.update(obj));
    }
    /**
     * 删除
     */
    public void del(){
      String ids=getPara("ids");
      Boolean b=userService.deleteByIds(ids);
      renderJson(b);
    }
    /**
     * 跳转到查看页面
     */
    public void view(){
    	Long id=Long.parseLong(getPara("userId"));//获取userId
        setAttr("user",userService.queryById(id));
        render("view.jsp");
    }
    
    /**
     * 主界面修改密码页面
     */
    public void toUpdPwd(){
    	User user=getSessionAttr(Constants.SESSION_USER_KEY);
    	setAttr("user",user);
    	render("toUpdPwd.jsp");
    }
    /**
     * 验证登陆用户原密码
     */
    public void checkPwd(){
    	User user=getSessionAttr(Constants.SESSION_USER_KEY);
    	String oUserPwd=MD5Util.getEncrypt(getPara("oUserPwd"));//获取密码并加密
    	if(user.getStr("password").equals(oUserPwd)){
    		renderJson(true);
    	}else{
    		renderJson(false);
    	}
    }
    /**
     * 修改密码
     */
    public void updPwd(){
    	String oldPwd=MD5Util.getEncrypt(getPara("oldPwd"));//原密码
    	String newPwd=MD5Util.getEncrypt(getPara("newPwd"));//新密码
    	User user=getSessionAttr(Constants.SESSION_USER_KEY);
    	if(!user.getStr("password").equals(oldPwd)){
    		renderText("原密码不正确");
    		return;
    	}else if(getPara("newPwd").equals("")||getPara("newPwd")==null){
    		renderText("新密码不能为空");
    		return;
    	}
    	//跟新用户，并更新session
    	user.set("password", newPwd);
    	userService.update(user);//跟新用户
    	setSessionAttr(Constants.SESSION_USER_KEY, user);//跟新session
    	renderText("succeed");//修改成功
    }
    /**
     * 验证用户名是否存在
     */
    public void checkUserName() {
    	paramMap=getParamMap();
    	Boolean b=userService.checkUserName(paramMap);
    	renderJson(b);
    }
}
